/**
 * 
 */
package br.fot.granola.genner.states.blinky;

import br.fot.granola.oboe.core.Oboe;
import br.fot.granola.oboe.core.Slot;
import br.fot.granola.oboe.graphics.Node;
import br.fot.granola.oboe.util.Vector;

/**
 * @author Felipe
 *
 */
public class Fly extends Node 
{
	private Vector target = new Vector();
	private Vector direction = new Vector();
	private Vector newTarget = new Vector();
	private Vector speed = new Vector();

	/**
	 * @param backend
	 */
	public Fly() {
		super( new SquareBackend() );
		this.scale.copy(0.05f);
		this.position.rand( 0, 2 );
		this.rotation.rand(0, 180);
		((SquareBackend)this.backend).color.set( 0, 0, 0, 1 );
	}

	@Slot public void onAddToParent( Object parent )
	{
		Oboe.signalUp(this, "getFlyTarget", this.target );
		this.target.y += 0.5f;
	}
	
	@Slot public void onFlyTargetChanged( Vector newTarget )
	{
		this.target.copy( newTarget ).y += 0.5f;
	}
	
	@Slot public void onUpdate()
	{
		this.direction.rand(-0.1f, 0.1f);
		this.newTarget.copy(position).add(direction);
		if( 
				(this.newTarget.x>this.target.x+0.5 && this.direction.x>0) ||
				(this.newTarget.x<this.target.x-0.5 && this.direction.x<0)
				) 
			this.direction.x = -this.direction.x;
		if( 
				(this.newTarget.y>this.target.y+2 && this.direction.y>0) ||
				(this.newTarget.y<this.target.y && this.direction.y<0)
				) 
			this.direction.y = -this.direction.y;
		if( 
				(this.newTarget.z>this.target.z+0.5 && this.direction.z>0) ||
				(this.newTarget.z<this.target.z-0.5 && this.direction.z<0)
				) 
			this.direction.z = -this.direction.z;
		this.speed.mul(0.85f).add( direction );
		this.newTarget.copy(speed);
		this.position.add( this.newTarget.mul(0.3f) );
		this.rotation.add( direction.mul(100.0f) );
	}
}
